package com.fenghongzhang.day008;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.hardware.Camera;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.tbruyelle.rxpermissions2.RxPermissions;

import java.io.File;
import java.io.IOException;

import io.reactivex.functions.Consumer;

public class Main3Activity extends AppCompatActivity implements SurfaceHolder.Callback{

    private static final String TAG = "Main3Activity";
    //MediaRecorder
    private MediaRecorder mediaRecorder; //这个对象的操作方法
    private Camera camera; //新
    private SurfaceView surfaceView;
    private SurfaceHolder surfaceHolder;
    private MediaPlayer mediaPlayer;


    private String path;
    private Button start;
    private Button stop;
    private Button play;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);


        surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
        start = (Button) findViewById(R.id.start);
        stop = (Button) findViewById(R.id.stop);
        play = (Button) findViewById(R.id.play);

        surfaceHolder =  surfaceView.getHolder();
        surfaceHolder.addCallback(this);

    }

    public void start(View view) {

        if(mediaRecorder == null){
            mediaRecorder = new MediaRecorder();
        }
        camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
        if(camera != null){
            camera.setDisplayOrientation(90);
            camera.unlock();
            mediaRecorder.setCamera(camera);
        }

        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
        mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);

//        Camera.Parameters parameters = camera.getParameters();
//        parameters.get

        mediaRecorder.setVideoSize(640 ,480);
        mediaRecorder.setVideoFrameRate(30);
        mediaRecorder.setVideoEncodingBitRate(1 * 1024 * 1024);
        mediaRecorder.setMaxDuration(30* 1000);
        mediaRecorder.setMaxFileSize(30 * 1024);
        mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());

        File file = new File(Environment.getExternalStorageDirectory() + File.separator + "video");
        if(!file.exists()){
            file.mkdir();
        }
         path = file.getAbsolutePath() +File.separator+ System.currentTimeMillis()+".mp4";
        Log.i(TAG, "start: "+path);
        mediaRecorder.setOutputFile(path);
        try {
            mediaRecorder.prepare();
            mediaRecorder.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void stop(View view) {
        mediaRecorder.stop();
        mediaRecorder.reset();
        mediaRecorder.release();
        mediaRecorder = null;
    }

    public void play(View view) {
        if(mediaPlayer == null){
            mediaPlayer = new MediaPlayer();
        }
        Uri parse = Uri.parse(path);
        mediaPlayer = MediaPlayer.create(this,parse);
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setDisplay(surfaceHolder);
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mediaPlayer.start();
            }
        });
    }

    @Override
    public void surfaceCreated(@NonNull SurfaceHolder holder) {
        surfaceHolder = holder;
    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
        surfaceHolder = holder;
    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
        surfaceHolder = null;
        surfaceView = null;
    }


}
